home *** CD-ROM | disk | FTP | other *** search
/ Software Explosion / Software Explosion (Fore-Matt Home Computing)(1996).iso / games / workbench / labyrinth_ii / console.c < prev    next >
C/C++ Source or Header  |  1988-10-04  |  6KB  |  236 lines

  1.  
  2. /* Console routines by Russell Wallace 1987, some obtained from other programs
  3.  * books etc. Feel free to copy them or use them in your own programs. Use
  4.  * +L option if compiling with Aztec C. */
  5.  
  6. #include "graphics/text.h"
  7. #include "graphics/gfxbase.h"
  8. #include "exec/exec.h"
  9. #include "devices/console.h"
  10. #include "devices/keymap.h"
  11. #include "libraries/dos.h"
  12. #include "intuition/intuition.h"
  13.  
  14. #define CloseConsole(x) CloseDevice(x)
  15. #define XOFFSET 26
  16. #define YOFFSET 39
  17.  
  18. extern struct Window *OpenWindow ();
  19. extern struct MsgPort *CreatePort ();
  20. extern struct IOStdReq *CreateStdIO ();
  21.  
  22. short row=0;
  23. short column=0;
  24. short font_width,font_height;
  25. int class,code;
  26. struct GfxBase *GfxBase=0;
  27. long IntuitionBase=0;
  28. struct NewWindow mynewwindow=
  29. {
  30.     0,0,        /* x,y co-ords of top left corner */
  31.     640,200,    /* start size */
  32.     0,1,        /* detailpen,blockpen */
  33.     MENUPICK|CLOSEWINDOW,    /* want to know if these happen */
  34.     ACTIVATE|GIMMEZEROZERO|WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE,
  35.     0,0,"Labyrinth II",0,0,
  36.     120,88,        /* minimum size */
  37.     640,256,    /* maximum size */
  38.     WBENCHSCREEN
  39. };
  40. struct IOStdReq *consoleWriteMsg=0;
  41. struct IOStdReq *consoleReadMsg=0;
  42. struct MsgPort *consoleWritePort=0;
  43. struct MsgPort *consoleReadPort=0;
  44. unsigned char letter;
  45. struct Window *mywindow=0;
  46. struct IntuiMessage *message;
  47.  
  48.     /* Start() must be called at beginning; returns window pointer - if 0,
  49.      * program has failed ... everything already cleaned up here, main ()
  50.      * must do its own cleaning up then exit */
  51.  
  52. struct Window *start ()
  53. {
  54.     if (!(GfxBase=(struct GfxBase *)OpenLibrary ("graphics.library",0)))
  55.         goto FAILED;
  56.     if (!(IntuitionBase=OpenLibrary ("intuition.library",0)))
  57.         goto FAILED;
  58.     if (!(mywindow=OpenWindow (&mynewwindow)))
  59.         goto FAILED;
  60.     if (!(consoleWritePort=CreatePort ("my.con.write",0)))
  61.         goto FAILED;
  62.     if (!(consoleReadPort=CreatePort ("my.con.read",0)))
  63.         goto FAILED;
  64.     if (!(consoleWriteMsg=CreateStdIO (consoleWritePort)))
  65.         goto FAILED;
  66.     if (!(consoleReadMsg=CreateStdIO (consoleReadPort)))
  67.         goto FAILED;
  68.     if (OpenConsole (consoleWriteMsg,consoleReadMsg,mywindow))
  69.         goto FAILED;
  70.     QueueRead (consoleReadMsg,&letter);
  71.     font_width=GfxBase->DefaultFont->tf_XSize;
  72.     font_height=GfxBase->DefaultFont->tf_YSize;
  73.     return (mywindow);
  74. FAILED:
  75.     closeall ();
  76.     return (0L);
  77. }
  78.  
  79.     /* Call finish() at the end IF start() was successful */
  80.  
  81. finish ()
  82. {
  83.     AbortIO (consoleReadMsg);
  84.     CloseConsole (consoleWriteMsg);
  85.     closeall ();
  86. }
  87.  
  88. closeall ()
  89. {
  90.     if (GfxBase)
  91.         CloseLibrary (GfxBase);
  92.     if (IntuitionBase)
  93.         CloseLibrary (IntuitionBase);
  94.     if (mywindow)
  95.         CloseWindow (mywindow);
  96.     if (consoleWritePort)
  97.         DeletePort (consoleWritePort);
  98.     if (consoleReadPort)
  99.         DeletePort (consoleReadPort);
  100.     if (consoleWriteMsg)
  101.         DeleteStdIO (consoleWriteMsg);
  102.     if (consoleReadMsg)
  103.         DeleteStdIO (consoleReadMsg);
  104. }
  105.  
  106. int OpenConsole (writerequest,readrequest,window)
  107. struct IOStdReq *writerequest,*readrequest;
  108. struct Window *window;
  109. {
  110.     register int error;
  111.     writerequest->io_Data=(APTR)window;
  112.     writerequest->io_Length=sizeof(*window);
  113.     error=OpenDevice ("console.device",0,writerequest,0);
  114.     readrequest->io_Device=writerequest->io_Device;
  115.     readrequest->io_Unit  =writerequest->io_Unit;
  116.     return (error);
  117. }
  118.  
  119. QueueRead (request,whereto)
  120. struct IOStdReq *request;
  121. char *whereto;
  122. {
  123.     request->io_Command=CMD_READ;
  124.     request->io_Data=(APTR)whereto;
  125.     request->io_Length=1;
  126.     SendIO (request);
  127. }
  128.  
  129.     /* Call checkinput() every so often to find out what the user is doing:
  130.      * a -ve number -1,-2 ... means the user has selected menu item 1,2 ...
  131.      * a +ve number means the user has typed a character, 1000 means the
  132.      * closewindow gadget has been selected, so do your ARE YOU SURE routine
  133.      * then call finish() and wind up. 0 means nothing's happened. */
  134.  
  135. long checkinput ()
  136. {
  137.     if (message=(struct IntuiMessage *)GetMsg (mywindow->UserPort))
  138.     {
  139.         class=message->Class;
  140.         code=message->Code;
  141.         ReplyMsg (message);
  142.         if (class==CLOSEWINDOW)
  143.             return (1000);
  144.         if (class==MENUPICK && code!=MENUNULL)
  145.             return (-1-ITEMNUM (code));
  146.     }
  147.     if (GetMsg (consoleReadPort))
  148.     {
  149.         code=letter;
  150.         QueueRead (consoleReadMsg,&letter);
  151.         return (code);
  152.     }
  153.     return (0);
  154. }
  155.  
  156.     /* Writechar() writes a character to the window ... to get a response from
  157.      * typed input, call it immediately with any characters returned from
  158.      * checkinput(). */
  159.  
  160. writechar (c)    /* NOTE - MUST BE CALLED WITH THE CHARACTER CAST TO TYPE LONG*/
  161. char c;            /* Don't blame me, I don't know why, blame the Amiga */
  162. {                /* systems programmers. */
  163.     consoleWriteMsg->io_Command=CMD_WRITE;
  164.     consoleWriteMsg->io_Data=(APTR)&c;
  165.     consoleWriteMsg->io_Length=1;
  166.     DoIO (consoleWriteMsg);
  167.     if (c=='\n')
  168.     {
  169.         column=0;
  170.         row++;
  171.     }
  172.     else
  173.         column++;
  174.     if ((row*font_height)+YOFFSET > mywindow->Height)
  175.     {
  176.         nprint ("MORE...");
  177.         do
  178.             ;
  179.         while (!checkinput ());
  180.         row=0;
  181.         nprint ("\r       \r");
  182.  
  183.     /* This is to prevent a large quantity of text from scrolling off the top
  184.      * of the window before the user has time to read it all e.g. in
  185.      * adventure games. Remove it if you don't need it. Note - don't let the
  186.      * user make the window too small vertically or an infinite loop occurs.*/
  187.  
  188.     }
  189. }
  190.  
  191.     /* Print() does a formatted (i.e. trying not to split up words on the ends
  192.      * of lines) print of a null-terminated string; very useful for text-
  193.      * oriented applications in a variable-sized window. */
  194.  
  195. print (string)
  196. char *string;
  197. {
  198.     short x;
  199.     for (;;)
  200.     {
  201.         x=0;
  202.         while (string[x]!='\0' && string[x]!=' ' && string[x]!='\n')
  203.             x++;
  204.         if (((column+x)*font_width)+XOFFSET > mywindow->Width && column)
  205.         {                            /* If word would go off end of line, */
  206.             writechar ((long)'\n');    /* go on to next line before print */
  207.         }
  208.         consoleWriteMsg->io_Command=CMD_WRITE;
  209.         consoleWriteMsg->io_Data=(APTR)string;
  210.         consoleWriteMsg->io_Length=x;
  211.         DoIO (consoleWriteMsg);
  212.         column+=x;
  213.         if (string[x]=='\n')
  214.             writechar ((long)'\n');
  215.         if (string[x]==' ')
  216.             writechar ((long)' ');
  217.         if (string[x]=='\0')
  218.             break;
  219.         string+=x+1;
  220.     }
  221. }
  222.  
  223. nprint (string)        /* Non-formatted print */
  224. char *string;
  225. {
  226.     consoleWriteMsg->io_Command=CMD_WRITE;
  227.     consoleWriteMsg->io_Data=(APTR)string;
  228.     consoleWriteMsg->io_Length=-1;
  229.     DoIO (consoleWriteMsg);
  230. }
  231.  
  232. resetscroll ()        /* Reset scroll pause feature */
  233. {
  234.     row=0;
  235. }
  236.